package leetcode.n2021;

/**
 * https://leetcode-cn.com/problems/counting-bits/description/
 * https://leetcode.com/problems/counting-bits/?tab=Description
 * @program: brushing
 * @description:
 * @author: LiuQiang
 * @create: 2018-07-25 10:41
 **/
public class CountingBits {
    public int[] countBits(int num) {
        int a[] = new int[num + 1];

        for (int i = 0; i <= num; i++) {
            String n = Integer.toBinaryString(i);
            int x = 0;
            String s[] = n.split("");
            for (int j = 0; j < s.length; j++) {
                if ("1".equals(s[j])) {
                    x++;
                }
            }
            a[i] = x;
        }

        return a;
    }

    public static void main(String[] args) {
        int a[] = new CountingBits().countBits(5);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}
